package com.daohang361.imqq.encoder;

import com.daohang361.imqq.Asycore.QQUser;
import com.daohang361.imqq.protocol.QQ2011;
import com.daohang361.imqq.tool.ByteArray;
import com.daohang361.imqq.tool.MyByteSendArray;
import com.daohang361.imqq.tool.MyUtil;
/**
 * 登陆解析
 * @author naner
 *
 */
public class LoginEncoder implements InterfaceEncoder {
	private boolean ignore=false;
	public  byte[] encode(QQUser myqq,int command) {
		int logintimes= myqq.getLogintimes();
		if(logintimes==0||logintimes==-1){//第一个发送包
			QQ2011.RandomKey();
			ByteArray myByte = new MyByteSendArray();
			myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_HEAD);
			myByte.putInt(QQ2011.QQ_PROTOCOL_VERSION);
			myByte.putHex("00");
			myByte.putInt(QQ2011.QQ_PROTOCOL_LOGIN_1);
			myByte.putInt(myqq.getPakage());//包增长应该在接受到此包后
			myByte.putInt(myqq.getQq());
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_12);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_key);
			myByte.saveThis();//保存此段作为第0段
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_39);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data1);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data2);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_59);
			myByte.saveThis();//保存此段作为第一段
			myByte.teaThis(1, 1, QQ2011.QQ_LOGIN_FIX_91_key);//加密第一段到第一段
			myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_TAIL);
			myByte.saveThis();//保存此段作为第二段
			return myByte.getByteList();//返回完整的package
		}else if(logintimes==1)//第二个0xba发送包
		{
			ByteArray myByte = new MyByteSendArray();
			myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_HEAD);
			myByte.putInt(QQ2011.QQ_PROTOCOL_VERSION);
			myByte.putHex("00");
			myByte.putInt(QQ2011.QQ_PROTOCOL_LOGIN_2);
			myByte.putInt(myqq.getPakage());
			myByte.putInt(myqq.getQq());
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_12);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_BA_key);
			myByte.saveThis();//保存此段作为第0段
			myByte.putHex("0001");
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data1	);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data2	);
			byte[] token_91_length = myqq.getAttr("token_91_length");//默认为"0038"
			myByte.putByte(token_91_length);
			byte[] Token91_data = myqq.getAttr("Token91_data");
			myByte.putByte(Token91_data);
			
			byte[] imgFlag =myqq.getAttr("imgFlag");
			int Squence=-1;
			if(imgFlag!=null&&MyUtil.bytes2Integer(imgFlag)!=QQ2011.QQ_LOGIN_BA_IMGEND_FLAG){//请求验证码图片包
				System.out.println("图片验证分包获取...");
				Squence=MyUtil.bytes2Integer(myqq.getAttr("Squence"));
				myByte.putByte(QQ2011.QQ_LOGIN_FIX_BA_REQUEST_PARTY_IMG);
				myByte.putByte(myqq.getAttr("imgDataLeng"+Squence));
				myByte.putByte(myqq.getAttr("imgData"+Squence));
			}else if(imgFlag!=null&&MyUtil.bytes2Integer(imgFlag)==QQ2011.QQ_LOGIN_BA_IMGEND_FLAG)//请求带验证码的包
			{
				System.out.println("图片验证回复...");
				Squence=MyUtil.bytes2Integer(myqq.getAttr("Squence"));
				myByte.putByte(QQ2011.QQ_LOGIN_FIX_BA_REQUEST_CHECK_IMG);
				myByte.putByte(myqq.getAttr("checkCodeLength"));//验证码长度
				myByte.putByte(myqq.getAttr("checkCode"));//验证码
				myByte.putByte(myqq.getAttr("token_ba_length"));
				myByte.putByte(myqq.getAttr("TokenBA_data"));
			}else{//普通发送包
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_BA_tail);
			}
			myByte.saveThis();
			myByte.teaThis(1, 1, QQ2011.QQ_LOGIN_FIX_BA_key);
			myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_TAIL);
			myByte.saveThis();
			return myByte.getByteList();
		}else if(logintimes==2){//0xdd登陆包
			ByteArray myByte = new MyByteSendArray();
			myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_HEAD);
			myByte.putInt(QQ2011.QQ_PROTOCOL_VERSION);
			myByte.putHex("00");
			myByte.putInt(QQ2011.QQ_PROTOCOL_LOGIN_3);
			myByte.putInt(myqq.getPakage());
			myByte.putInt(myqq.getQq());
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_12);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_DD_THIS_KEY);
			myByte.saveThis();//保存此段作为第0段
			
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_DD_43);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data1);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data2);
			myByte.putHex("0038");//56字节tokenba_data
			byte[] TokenBA_data = myqq.getAttr("TokenBA_data");
			myByte.putByte(TokenBA_data);
			myByte.putHex("0078");//120字节密码验证
			myByte.saveThis();//保存此段作为第1段
			
			myByte.putByte(MyUtil.getRandom(4));//放入一个4位随机数
			myByte.putHex("0001");
			myByte.putInt(myqq.getQq());//qq号码
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data2);
			myByte.putHex("000001");
			myByte.putByte(myqq.getMd51());//密码的一次MD5值
			myByte.putByte(myqq.getAttr("loginTime"));//登陆时间
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_DD_00_L13);
			myByte.putByte(myqq.getAttr("loginIp"));
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_DD_00_L8);
			myByte.putHex("0010"); //数据长度，16字节
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_DD_INNER);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_DD_NEXT_KEY);//这个是0xDD接收包解密key
			myByte.saveThis();//保存此段作为第2段
			
			myByte.putHex("0014");
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_DD_L30);//md52次后第一段
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_DD_KEY1);//0xDD固定Key1
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_BETWEEN_KEY1_KEY2);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_DD_KEY2);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_DD_321);
			myByte.saveThis();//保存作为第3段
			
			myByte.teaThis(2, 2, myqq.getMd52());//先内部加密
			myByte.teaThis(1, 3, QQ2011.QQ_LOGIN_FIX_DD_THIS_KEY);//再外围加密
			myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_TAIL);//添加尾巴
			myByte.saveThis();
			return myByte.getByteList();
		}else if(logintimes==3){//0xe5登陆包
			ByteArray myByte = new MyByteSendArray();
			myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_HEAD);
			myByte.putInt(QQ2011.QQ_PROTOCOL_VERSION);
			myByte.putHex("00");
			myByte.putInt(QQ2011.QQ_PROTOCOL_LOGIN_4);
			myByte.putInt(myqq.getPakage());
			myByte.putInt(myqq.getQq());
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_12);
			myByte.putHex("0038");
			myByte.putByte(myqq.getAttr("TokenDD_3"));
			myByte.saveThis();
			
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_E5_80);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data1);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data2);
			myByte.putHex("0038");
			myByte.putByte(myqq.getAttr("TokenBA_data"));
			myByte.putHex("0020");
			myByte.putByte(myqq.getAttr("TokenDD_1"));
			myByte.putHex("00980002");//
			myByte.putByte(myqq.getAttr("loginTime"));
			myByte.putHex("0090");
			myByte.putByte(myqq.getAttr("TokenDD_2"));
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_E5_tail);
			myByte.saveThis();
			
			myByte.teaThis(1, 1, myqq.getAttr("TokenDD_key1"));//加密
			myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_TAIL);//添加尾巴
			myByte.saveThis();
			return myByte.getByteList();
		}else if(logintimes==4){//0xe3登陆包
			ByteArray myByte = new MyByteSendArray();
			myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_HEAD);
			myByte.putInt(QQ2011.QQ_PROTOCOL_VERSION);
			myByte.putHex("00");
			myByte.putInt(QQ2011.QQ_PROTOCOL_LOGIN_5);
			myByte.putInt(myqq.getPakage());
			myByte.putInt(myqq.getQq());
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_12);
			myByte.putHex("0038");
			myByte.putByte(myqq.getAttr("TokenDD_3"));
			myByte.saveThis();
			
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_E3_25);//实际从这开始加密的刚刚这错了，应该是这，这俩个解密都一样
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data1);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data2);
			myByte.putHex("0038");
			myByte.putByte(myqq.getAttr("TokenBA_data"));
			myByte.putHex("0078");
			myByte.putByte(myqq.getAttr("TokenE5_data2"));
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_E3_TAIL);
			
			myByte.saveThis();//这个是一段
			myByte.teaThis(1, 1, myqq.getAttr("TokenDD_key1"));
			myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_TAIL);//添加尾巴
			myByte.saveThis();
			return myByte.getByteList();
		}else if(logintimes==5){//0x30
			ByteArray myByte = new MyByteSendArray();
			myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_HEAD);
			myByte.putInt(QQ2011.QQ_PROTOCOL_VERSION);
			myByte.putHex("00");
			myByte.putInt(QQ2011.QQ_PROTOCOL_LOGIN_6);
			myByte.putInt(myqq.getPakage());
			myByte.putInt(myqq.getQq());
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_12);
			myByte.putHex("0038");
			myByte.putByte(myqq.getAttr("TokenDD_3"));
			myByte.saveThis();
			
			myByte.putHex("0001");
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data2);
			myByte.putByte(myqq.getAttr("TokenE5_fix"));//固定（00E5中接收）
			myByte.putByte(myqq.getAttr("loginTime"));
			myByte.putByte(myqq.getAttr("loginIp"));
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_DD_00_L8);
			myByte.putHex("00D0");
			myByte.putByte(myqq.getAttr("TokenE5_data1"));//00E5_Token1
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_30_3);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_30_3);
			myByte.putHex("000000");
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_30_1);
			myByte.putHex("140A");//随机2字节
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_30_2);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_91_data1);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_30_3);
			myByte.putHex("0038");
			myByte.putByte(myqq.getAttr("TokenBA_data"));
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_30_4);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_30_5);
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_BETWEEN_KEY1_KEY2);
			
			myByte.putByte(myqq.getAttr("TokenDD_key1"));
			myByte.putByte(QQ2011.QQ_LOGIN_FIX_30_6);
			myByte.saveThis();
			
			myByte.teaThis(1, 1,  myqq.getAttr("TokenDD_key1"));
			myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_TAIL);//添加尾巴
			myByte.saveThis();
			return myByte.getByteList();
		}
		return null;
	}

	@Override
	public boolean getIgnore() {
		
		return ignore;
	}
	
	@Override
	public void setIgnore(boolean ignore) {
		this.ignore=ignore;
	}

	@Override
	public Object invoke(Object... o) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public byte[] encode(QQUser myqq, int command, Object... o) {
		// TODO Auto-generated method stub
		return null;
	}
}
